﻿function BenefitViewModel(model) {
    var selfModel = this;
    var isMain = false;

    selfModel.Controller = "Benefit";
    selfModel.StartPage = selfModel.Controller + "Create";
    
    ko.mapping.fromJS(model, {}, selfModel);

    selfModel.prototype = new CRUDViewModel(selfModel);

    selfModel.List = ko.observableArray();

    Main.State.CurrentPage.subscribe(function (newValue) {
        if (newValue == selfModel.Controller) {
            Main.Page.ChangeHash(selfModel.StartPage);
        }
    });

    selfModel.currentProjectId = ko.observable();

    selfModel.ClearList = function (id) {
        selfModel.List.removeAll();
    }

    // Limpio la colección y seteo el id.
    selfModel.setProjectId = function (id) {
        selfModel.currentProjectId = id;
        selfModel.ClearList();
    };

    selfModel.CreateOnJavascript = function () {
        var newEntity = ko.toJS(model.Entity);;
        selfModel.List.push(newEntity);
        selfModel.ResetForm();
    };

    selfModel.DeleteOnJavascript = function (item) {
        selfModel.List.remove(item);
    };

    selfModel.Init(isMain);
}